package com.ljc.controller;

import com.alibaba.fastjson.JSONObject;
import com.ljc.entity.BaseResponse;
import com.ljc.entity.PageResult;
import com.ljc.entity.User;
import com.ljc.entity.UserInfo;
import com.ljc.service.UserFollowingService;
import com.ljc.service.UserService;
import com.ljc.support.UserSupport;
import com.ljc.utils.RSAUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 用户相关
 */
@RestController
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UserFollowingService userFollowingService;
    @Resource
    private UserSupport userSupport;

    /**
     * 从token中获取userId，根据userId查询用户信息并返回
     */
    @GetMapping("/users")
    public BaseResponse<User> getUserInfo() {
        Long userId = userSupport.getCurrentUserId();
        User user = userService.getUserInfo(userId);
        return new BaseResponse<>(user);
    }

    /**
     * 获取 RSA 公钥
     */
    @GetMapping("/rsa-pks")
    private BaseResponse<String> getRsaPublicKey() {
        String pk = RSAUtil.getPublicKeyStr();
        return new BaseResponse<>(pk);
    }

    /**
     * 注册接口
     */
    @PostMapping("/users")
    private BaseResponse<String> addUser(@RequestBody User user) {
        userService.addUser(user);
        return BaseResponse.success();
    }

    /**
     * 登录接口
     */
    @PostMapping("/user-tokens")
    private BaseResponse<String> login(@RequestBody User user) throws Exception {
        String token = userService.login(user);
        return new BaseResponse<>(token);
    }

    /**
     * 更新用户
     */
    @PutMapping("/users")
    public BaseResponse<String> updateUsers(@RequestBody User user) throws Exception {
        Long userId = userSupport.getCurrentUserId();
        user.setId(userId);
        userService.updateUsers(user);
        return BaseResponse.success();
    }

    /**
     * 更新用户信息接口
     */
    @PutMapping("/user-infos")
    public BaseResponse<String> updateUserInfos(@RequestBody UserInfo userInfo) {
        Long userId = userSupport.getCurrentUserId();
        userInfo.setUserId(userId);
        userService.updateUserInfos(userInfo);
        return BaseResponse.success();
    }

    /**
     * 分页查询用户
     */
    @GetMapping("/user-infos")
    public BaseResponse<PageResult<UserInfo>> pageListUserInfos(@RequestParam Integer no, @RequestParam Integer size, String nick) {
        Long userId = userSupport.getCurrentUserId();
        JSONObject params = new JSONObject();
        params.put("no", no);
        params.put("size", size);
        params.put("nick", nick);
        params.put("userId", userId);
        PageResult<UserInfo> result = userService.pageListUserInfos(params);
        if (result.getTotal() > 0) {
            List<UserInfo> checkedUserInfoList = userFollowingService.checkFollowingStatus(result.getList(), userId);
            result.setList(checkedUserInfoList);
        }
        return new BaseResponse<>(result);
    }

    /**
     * 双token登录，存储refresh-token到数据库
     */
    @PostMapping("/user-dts")
    public BaseResponse<Map<String, Object>> loginForDts(@RequestBody User user) throws Exception {
        Map<String, Object> map = userService.loginForDts(user);
        return new BaseResponse<>(map);
    }

    /**
     * 用户退出则将refresh-token设置为无效
     */
    @DeleteMapping("/refresh-tokens")
    public BaseResponse<String> logout(HttpServletRequest request) {
        String refreshToken = request.getHeader("refreshToken");
        Long userId = userSupport.getCurrentUserId();
        userService.logout(refreshToken, userId);
        return BaseResponse.success();
    }

    /**
     * 刷新access-token
     */
    @PostMapping("/access-tokens")
    public BaseResponse<String> refreshAccessToken(HttpServletRequest request) throws Exception {
        String refreshToken = request.getHeader("refreshToken");
        String accessToken = userService.refreshAccessToken(refreshToken);
        return new BaseResponse<>(accessToken);
    }
}
